package ca.ualberta.cs.CLRServer.resources;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import ca.ualberta.cs.CLRDB.User;
import ca.ualberta.cs.CLRServer.Interface.SecureResource;
import ca.ualberta.cs.CLRServer.Interface.ServerInterface;

@Path("/publishService/friends")
public class FriendsController extends SecureResource{
		
	@Path("/request")
	@POST 
    @Consumes("application/json")
    //@Produces("text/plain")
    public String requestFriendship(List<JAXBBean> requestArr){
    	    	
		checkBufferOverflow(requestArr,3);
		checkRequiredKeys(requestArr, "requester","requested","password");

		String requester = null, pass = null, 
				requested = null;
		
		for(int i = 0 ; i < requestArr.size() ; i++ ){
    		if( requestArr.get(i).getKey().equals("password") )
    			pass = requestArr.get(i).getVal();
    		else if( requestArr.get(i).getKey().equals("requester") )
    			requester = requestArr.get(i).getVal();    		
    		else if( requestArr.get(i).getKey().equals("requested"))
    			requested = requestArr.get(i).getVal();    		
    	}
		    	
		System.out.println("\n--------------------Request Friendship---------------------");
		System.out.println("requester: "+requester);
		System.out.println("requested: "+requested);
		System.out.println("-----------------------------------------");
		System.out.println();
		
    	return String.valueOf(ServerInterface.db.getPersonsManagement().getFriendsManagement().requestFriendship(requester, pass, requested));    	
    }

	
	@Path("/follow")
	@POST 
    @Consumes("application/json")
    //@Produces("text/plain")
    public String requestFollow(List<JAXBBean> requestArr){
    	    	
		checkBufferOverflow(requestArr,3);
		checkRequiredKeys(requestArr, "requester","requested","password");

		String requester = null, pass = null, 
				requested = null;
		
		for(int i = 0 ; i < requestArr.size() ; i++ ){
    		if( requestArr.get(i).getKey().equals("password") )
    			pass = requestArr.get(i).getVal();
    		else if( requestArr.get(i).getKey().equals("requester") )
    			requester = requestArr.get(i).getVal();    		
    		else if( requestArr.get(i).getKey().equals("requested"))
    			requested = requestArr.get(i).getVal();    		
    	}
		    	
		System.out.println("\n--------------------Request to Follow---------------------");
		System.out.println("requester: "+requester);
		System.out.println("requested: "+requested);
		System.out.println("-----------------------------------------");
		System.out.println();
		
    	return String.valueOf(ServerInterface.db.getPersonsManagement().getFriendsManagement().follow(requester, pass, requested));    	
    }

	
	@Path("/accept")
	@POST 
    @Consumes("application/json")
    //@Produces("text/plain")
    public String acceptFriendship(List<JAXBBean> requestArr){
    	    	
		checkBufferOverflow(requestArr,3);
		checkRequiredKeys(requestArr, "requester","requested","password");

		String requester = null, pass = null, 
				requested = null;
		
		for(int i = 0 ; i < requestArr.size() ; i++ ){
    		if( requestArr.get(i).getKey().equals("password") )
    			pass = requestArr.get(i).getVal();
    		else if( requestArr.get(i).getKey().equals("requester") )
    			requester = requestArr.get(i).getVal();    		
    		else if( requestArr.get(i).getKey().equals("requested"))
    			requested = requestArr.get(i).getVal();    		
    	}
		    	
		System.out.println("\n--------------------Accept Friendship---------------------");
		System.out.println("requester: "+requester);
		System.out.println("requested: "+requested);
		System.out.println("-----------------------------------------");
		System.out.println();
		
    	return String.valueOf(ServerInterface.db.getPersonsManagement().getFriendsManagement().acceptFriendship(requested, pass, requester));    	
    }

	@Path("/listFriends")
	@POST 
    @Consumes("application/json")
    @Produces("application/json")
    public List<User> listFriends(List<JAXBBean> requestArr){
    	    	
		checkBufferOverflow(requestArr,3);
		checkRequiredKeys(requestArr, "username","password");

		String username = null, pass = null;
		
		for(int i = 0 ; i < requestArr.size() ; i++ ){
    		if( requestArr.get(i).getKey().equals("password") )
    			pass = requestArr.get(i).getVal();
    		else if( requestArr.get(i).getKey().equals("username") )
    			username = requestArr.get(i).getVal();    		    		    	
    	}
		    	
		System.out.println("\n--------------------List Friendship For:---------------------");
		System.out.println("username: "+username);
		System.out.println("-----------------------------------------");
		System.out.println();
		
    	return ServerInterface.db.getPersonsManagement().getFriendsManagement().listOfFriends(username, pass);
    }

	@Path("/listRequests")
	@POST 
    @Consumes("application/json")
    @Produces("application/json")
    public List<User> listRequests(List<JAXBBean> requestArr){
    	    	
		checkBufferOverflow(requestArr,3);
		checkRequiredKeys(requestArr, "username","password");

		String username = null, pass = null;
		
		for(int i = 0 ; i < requestArr.size() ; i++ ){
    		if( requestArr.get(i).getKey().equals("password") )
    			pass = requestArr.get(i).getVal();
    		else if( requestArr.get(i).getKey().equals("username") )
    			username = requestArr.get(i).getVal();    		    		    	
    	}
		    	
		System.out.println("\n--------------------List Friendship Requests For:---------------------");
		System.out.println("username: "+username);
		System.out.println("-----------------------------------------");
		System.out.println();
		
    	return ServerInterface.db.getPersonsManagement().getFriendsManagement().listFriendshipRequests(username, pass);
    }

	
}
